<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="营地用户类型:" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择营地用户类型" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_usertype_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地类型" prop="campType">
        <el-select v-model="queryParams.campType" placeholder="请选择营地类型:" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_camptype_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地名称；" prop="campName">
        <el-input
          v-model="queryParams.campName"
          placeholder="请输入营地名称；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营地经度；" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入营地经度；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营地维度；" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入营地维度；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营地位置；" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入营地位置；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="具体地址；" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入具体地址；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业时间类型:" prop="timeType">
        <el-select v-model="queryParams.timeType" placeholder="请选择营业时间类型:" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_timetype_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人；" prop="linkman">
        <el-input
          v-model="queryParams.linkman"
          placeholder="请输入联系人；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话；" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_state_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核" prop="approve">
        <el-select v-model="queryParams.approve" placeholder="请选择审核" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_approve_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="置顶首页" prop="show">
        <el-select v-model="queryParams.show" placeholder="请选择置顶首页" clearable>
          <el-option
            v-for="dict in dict.type.camp_camp_show_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评分；" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入评分；"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['camp:camp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['camp:camp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['camp:camp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['camp:camp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="campList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="营地Id" align="center" prop="campId" />
      <el-table-column label="营地用户类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_usertype_status" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="营地类型" align="center" prop="campType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_camptype_status" :value="scope.row.campType"/>
        </template>
      </el-table-column>
      <el-table-column label="营地名称；" align="center" prop="campName" />
      <el-table-column label="营地经度；" align="center" prop="longitude" />
      <el-table-column label="营地维度；" align="center" prop="latitude" />
      <el-table-column label="营地位置；" align="center" prop="location" />
      <el-table-column label="具体地址；" align="center" prop="address" />
      <el-table-column label="营业时间类型" align="center" prop="timeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_timetype_status" :value="scope.row.timeType"/>
        </template>
      </el-table-column>
      <el-table-column label="具体时间" align="center" prop="specificTime">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_specifictime_status" :value="scope.row.specificTime ? scope.row.specificTime.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人；" align="center" prop="linkman" />
      <el-table-column label="电话；" align="center" prop="phone" />
      <el-table-column label="营地图片；" align="center" prop="imageUrl" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_state_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="营地介绍" align="center" prop="introduce" />
      <el-table-column label="审核" align="center" prop="approve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_approve_status" :value="scope.row.approve"/>
        </template>
      </el-table-column>
      <el-table-column label="置顶首页" align="center" prop="show">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camp_show_status" :value="scope.row.show"/>
        </template>
      </el-table-column>
      <el-table-column label="评分；" align="center" prop="score" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['camp:camp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['camp:camp:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营地管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="营地用户类型" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择营地用户类型">
            <el-option
              v-for="dict in dict.type.camp_camp_usertype_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营地类型" prop="campType">
          <el-select v-model="form.campType" placeholder="请选择营地类型">
            <el-option
              v-for="dict in dict.type.camp_camp_camptype_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营地名称；" prop="campName">
          <el-input v-model="form.campName" placeholder="请输入营地名称；" />
        </el-form-item>
        <el-form-item label="营地经度；" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入营地经度；" />
        </el-form-item>
        <el-form-item label="营地维度；" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入营地维度；" />
        </el-form-item>
        <el-form-item label="营地位置；" prop="location">
          <el-input v-model="form.location" placeholder="请输入营地位置；" />
        </el-form-item>
        <el-form-item label="具体地址；" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体地址；" />
        </el-form-item>
        <el-form-item label="营业时间类型" prop="timeType">
          <el-select v-model="form.timeType" placeholder="请选择营业时间类型">
            <el-option
              v-for="dict in dict.type.camp_camp_timetype_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="具体时间" prop="specificTime">
          <el-checkbox-group v-model="form.specificTime">
            <el-checkbox
              v-for="dict in dict.type.camp_specifictime_status"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="联系人；" prop="linkman">
          <el-input v-model="form.linkman" placeholder="请输入联系人；" />
        </el-form-item>
        <el-form-item label="电话；" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话；" />
        </el-form-item>
        <el-form-item label="营地图片；" prop="imageUrl">
          <el-input v-model="form.imageUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.camp_camp_state_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="营地介绍" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核" prop="approve">
          <el-select v-model="form.approve" placeholder="请选择审核">
            <el-option
              v-for="dict in dict.type.camp_camp_approve_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="置顶首页" prop="show">
          <el-select v-model="form.show" placeholder="请选择置顶首页">
            <el-option
              v-for="dict in dict.type.camp_camp_show_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评分；" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分；" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCamp, getCamp, delCamp, addCamp, updateCamp } from "@/api/camp/camp";

export default {
  name: "Camp",
  dicts: ['camp_camp_state_status', 'camp_camp_timetype_status', 'camp_camp_show_status', 'camp_camp_camptype_status', 'camp_camp_usertype_status', 'camp_specifictime_status', 'camp_camp_approve_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营地管理表格数据
      campList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: null,
        campType: null,
        campName: null,
        longitude: null,
        latitude: null,
        location: null,
        address: null,
        timeType: null,
        specificTime: null,
        linkman: null,
        phone: null,
        imageUrl: null,
        state: null,
        introduce: null,
        approve: null,
        show: null,
        score: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userType: [
          { required: true, message: "营地用户类型", trigger: "change" }
        ],
        campType: [
          { required: true, message: "营地类型", trigger: "change" }
        ],
        campName: [
          { required: true, message: "营地名称；不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "营地经度；不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "营地维度；不能为空", trigger: "blur" }
        ],
        timeType: [
          { required: true, message: "营业时间类型空", trigger: "change" }
        ],
        specificTime: [
          { required: true, message: "具体时间", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话；不能为空", trigger: "blur" }
        ],
        imageUrl: [
          { required: true, message: "营地图片；不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态", trigger: "change" }
        ],
        introduce: [
          { required: true, message: "营地介绍不能为空", trigger: "blur" }
        ],
        approve: [
          { required: true, message: "审核", trigger: "change" }
        ],
        show: [
          { required: true, message: "置顶首页", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询营地管理列表 */
    getList() {
      this.loading = true;
      listCamp(this.queryParams).then(response => {
        this.campList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        campId: null,
        userType: null,
        campType: null,
        campName: null,
        longitude: null,
        latitude: null,
        location: null,
        address: null,
        timeType: null,
        specificTime: [],
        linkman: null,
        phone: null,
        imageUrl: null,
        state: null,
        introduce: null,
        approve: null,
        show: null,
        score: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.campId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营地管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const campId = row.campId || this.ids
      getCamp(campId).then(response => {
        this.form = response.data;
        this.form.specificTime = this.form.specificTime.split(",");
        this.open = true;
        this.title = "修改营地管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.specificTime = this.form.specificTime.join(",");
          if (this.form.campId != null) {
            updateCamp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("添加数据：", this.form);
            addCamp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const campIds = row.campId || this.ids;
      this.$modal.confirm('是否确认删除营地管理编号为"' + campIds + '"的数据项？').then(function() {
        return delCamp(campIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('camp/camp/export', {
        ...this.queryParams
      }, `camp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
